home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / seg_tree.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  8.8 KB  |  318 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  seg_tree.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_SEGMENT_TREE_H
  16. #define LEDA_SEGMENT_TREE_H
  17.  
  18. // ------------------------------------------------------------------
  19. //
  20. // full dynamic Segment Trees
  21. //
  22. // Michael Wenzel     (1990)
  23. //
  24. // Implementation follows
  25. // Kurt Mehlhorn: Data Structures and Algorithms, Vol. 3
  26. //
  27. // ------------------------------------------------------------------
  28.  
  29. #include <LEDA/impl/bb_tree1.h>
  30. #include <LEDA/list.h>
  31.  
  32. // ------------------------------------------------------------------
  33. // declarations and definitions 
  34. // ----------------------------------------------------------------- 
  35.  
  36.  
  37. class h_segment;
  38. typedef h_segment* h_segment_p;
  39.  
  40. class seg_node_tree;
  41. typedef seg_node_tree* seg_node_list;
  42.  
  43.  
  44. typedef bb_node* seg_tree_item;
  45. typedef list<seg_tree_item> list_seg_tree_item_;
  46.  
  47. //------------------------------------------------------------------
  48. // class h_segment
  49. //------------------------------------------------------------------
  50.  
  51. class h_segment {
  52.  
  53.   GenPtr _x0;
  54.   GenPtr _x1;
  55.   GenPtr _y;
  56.   GenPtr _inf;
  57.  
  58.  public:
  59.  
  60.  GenPtr& x0()    { return _x0;   }
  61.  GenPtr& x1()    { return _x1;   }
  62.  GenPtr& y()     { return _y;    }
  63.  GenPtr& info()  { return _inf;  }
  64.  
  65.  h_segment()
  66.    _x0 = _x1 = _y = _inf = 0;
  67.  }
  68.  
  69.  h_segment(GenPtr x0, GenPtr x1, GenPtr y, GenPtr i=0)
  70.    _x0  = x0;
  71.    _x1  = x1;
  72.    _y   = y;
  73.    _inf = i;
  74.  }
  75.  
  76.  LEDA_MEMORY(h_segment)
  77.  
  78.  friend ostream& operator<<(ostream&, h_segment&);
  79.  friend class Segment_Tree;
  80.  friend class seg_node_tree;
  81.  
  82. };
  83.  
  84.  
  85. /*------------------------------------------------------------------
  86.    class seg_node_tree = Dictionary(seg_tree_item , void* )
  87. -------------------------------------------------------------------*/
  88.  
  89. class seg_node_tree : public bb_tree {
  90.  
  91. public:
  92.  
  93. Segment_Tree* father;
  94.  
  95. int cmp(GenPtr x, GenPtr y) const;
  96.  
  97. list<seg_tree_item> query(GenPtr, GenPtr);
  98. list<seg_tree_item> all_items();
  99.  
  100. int            defined(h_segment_p y)    { return bb_tree::member(Convert(y)); }
  101. seg_tree_item  lookup(h_segment_p y)     { return bb_tree::lookup(Convert(y)); }
  102. seg_tree_item  locate(h_segment_p y)     { return bb_tree::locate(Convert(y)); }
  103. seg_tree_item  ord(int y)                { return bb_tree::ord(int(y)); }
  104. seg_tree_item  insert(h_segment_p y, GenPtr i=0 )
  105.                                  { return bb_tree::insert(Convert(y),i); } 
  106. void del(h_segment_p y)          { delete bb_tree::del(Convert(y)); } 
  107. void del_item(seg_tree_item it)  { del(key(it)); } 
  108.  
  109. h_segment_p& key(seg_tree_item it)   
  110.          { if (!it) error_handler(1,"seg_tree_item gleich nil");
  111.                return (h_segment_p&)it->ke  ; }
  112. GenPtr&   info(seg_tree_item it)              { return key(it)->info(); } 
  113. void         change_inf(seg_tree_item it, GenPtr i) { key(it)->info() = i; }
  114.  
  115. seg_node_tree(Segment_Tree* p)   {father = p;}
  116. virtual ~seg_node_tree()  {}
  117.  
  118. friend class Segment_Tree;
  119.  
  120. } ;
  121.  
  122.  
  123. #define forall_seg_tree_items(a,b) for ((b).init_iterator(); a=(b).move_iterator(); )
  124.  
  125.  
  126.  
  127. //------------------------------------------------------------------
  128. // class segment_tree
  129. //------------------------------------------------------------------
  130.  
  131. class Segment_Tree  : public bb_tree {
  132.  
  133.  
  134. virtual  h_segment_p new_y_h_segment(GenPtr y)
  135. { cout << "error: virtual new_y_h_segmentn"; y=0; return 0; }
  136.  
  137. virtual int cmp_dim1(GenPtr x,GenPtr y) {return compare(x,y);}
  138. virtual int cmp_dim2(GenPtr x,GenPtr y) {return compare(x,y);}
  139.  
  140. virtual void clear_dim1(GenPtr&) {}
  141. virtual void clear_dim2(GenPtr&) {}
  142. virtual void clear_info(GenPtr&) {}
  143. virtual void copy_dim1(GenPtr&)  {}
  144. virtual void copy_dim2(GenPtr&)  {}
  145. virtual void copy_info(GenPtr&)  {}
  146.  
  147. int seg_cmp(h_segment_p p, h_segment_p q);
  148.  
  149.   void lrot(bb_item , bb_item);
  150.   void rrot(bb_item , bb_item);
  151.   void ldrot(bb_item , bb_item);
  152.   void rdrot(bb_item , bb_item);
  153.  
  154.   //void change_inf(bb_item it, seg_node_list i)   { info(it) = i; }
  155.  
  156.   GenPtr& key(bb_item it)       
  157.        { if (!it) error_handler(1,"bb_item in segment_tree gleich nil");
  158.      return it->ke; }
  159.   seg_node_list& info(bb_item it)    { return (seg_node_list&)(bb_tree::info(it)); } 
  160.  
  161.   int start_coord(bb_item& x,seg_tree_item& i)
  162.       { return (!cmp(key(x),x0(i))); }
  163.   int end_coord(bb_item& x,seg_tree_item& i)
  164.       { return (!cmp(key(x),x1(i))); }
  165.  
  166.   int empty(bb_item);
  167.   void clear(bb_item& );
  168.   void print(bb_item , string); 
  169.  
  170.   protected:
  171.  
  172.   seg_node_tree r;                // tree with all segments
  173.  
  174.  
  175.   int cmp_dummy(int a, int b, int c);
  176.  
  177.  
  178.   public :
  179.   
  180.   int cmp(GenPtr, GenPtr)  const    
  181.   { cout << "error: Segment_Tree::cmpn"; return 0; }
  182.  
  183.   GenPtr x0(seg_tree_item x)    { return (r.key(x))->_x0;  }
  184.   GenPtr x1(seg_tree_item x)    { return (r.key(x))->_x1;  }
  185.   GenPtr y(seg_tree_item x)     { return (r.key(x))->_y;   }
  186.   GenPtr& inf(seg_tree_item x)  { return r.info(x);        }
  187.  
  188.   GenPtr& x0(h_segment_p x)   { return x->_x0; }
  189.   GenPtr& x1(h_segment_p x)   { return x->_x1; }
  190.   GenPtr& y(h_segment_p x)    { return x->_y; }
  191.   GenPtr& inf(h_segment_p x)  { return x->_inf; }
  192.  
  193.   void change_inf(seg_tree_item x, GenPtr i)  { r.info(x) = i; }
  194.  
  195.   seg_tree_item insert(GenPtr, GenPtr, GenPtr, GenPtr i=0 );
  196.  
  197.   void del(GenPtr, GenPtr, GenPtr);
  198.   void del_item(seg_tree_item it) { del(x0(it),x1(it),y(it)) ; }
  199.  
  200.  
  201.   seg_tree_item lookup(GenPtr, GenPtr, GenPtr );
  202.   int member(GenPtr x0, GenPtr x1, GenPtr y) { return (lookup(x0,x1,y)!=0 ) ; }
  203.  
  204.   list<seg_tree_item> query(GenPtr, GenPtr, GenPtr );
  205.   list<seg_tree_item> x_infinity_query(GenPtr, GenPtr );
  206.   list<seg_tree_item> y_infinity_query(GenPtr );
  207.   list<seg_tree_item> all_items();
  208.  
  209.   void clear_tree();  
  210.  
  211.    Segment_Tree();
  212.    virtual ~Segment_Tree();
  213.  
  214.   int size()                   { return r.size();   }
  215.   int empty()                  { return (r.size()==0) ; }
  216.  
  217.   seg_tree_item y_min()        { return r.min();    }
  218.   seg_tree_item y_max()        { return r.max();    }
  219.  
  220.   void init_iterator()            { r.init_iterator(); }
  221.   seg_tree_item move_iterator()   { return r.move_iterator(); }
  222.  
  223.   void print_tree()               { print(root,"");    }
  224.  
  225.  
  226.   friend class seg_node_tree;
  227.  
  228. };
  229.  
  230.  
  231. //------------------------------------------------------------------
  232. // typed segment_tree
  233. //------------------------------------------------------------------
  234.  
  235.  
  236.  
  237. template <class  type1, class type2, class itype>
  238.  
  239. class _CLASSTYPE segment_tree : public Segment_Tree {
  240.  
  241. h_segment_p new_y_h_segment(GenPtr y)
  242. { type1 x1; type2 x2;
  243.   Init(x1); Init(x2);
  244.   return new h_segment(Copy(x1),Copy(x2),y);
  245.  }
  246.  
  247. int cmp_dim1(GenPtr x,GenPtr y)
  248. { return compare(ACCESS(type1,x),ACCESS(type1,y)); }
  249.  
  250. int cmp_dim2(GenPtr x,GenPtr y)
  251. { return compare(ACCESS(type2,x),ACCESS(type2,y)); }
  252.  
  253. void clear_dim1(GenPtr& x)     { Clear(ACCESS(type1,x)); }
  254. void clear_dim2(GenPtr& x)     { Clear(ACCESS(type2,x)); }
  255. void clear_info(GenPtr& x)     { Clear(ACCESS(itype,x)); }
  256.  
  257. void copy_dim1(GenPtr& x)     { x=Copy(ACCESS(type1,x)); }
  258. void copy_dim2(GenPtr& x)     { x=Copy(ACCESS(type2,x)); }
  259. void copy_info(GenPtr& x)     { x=Copy(ACCESS(itype,x)); }
  260.  
  261. int cmp(GenPtr x, GenPtr y) const
  262. { return compare(ACCESS(type1,x),ACCESS(type1,y));}
  263.  
  264. public:
  265.  
  266. type1  x0(seg_tree_item it)  { return ACCESS(type1,Segment_Tree::x0(it)); }
  267. type1  x1(seg_tree_item it)  { return ACCESS(type1,Segment_Tree::x1(it)); }
  268. type2   y(seg_tree_item it)  { return ACCESS(type2,Segment_Tree::y(it));  }
  269. itype inf(seg_tree_item it)  { return ACCESS(itype,Segment_Tree::inf(it));}
  270.  
  271. seg_tree_item insert(type1 x0, type1 x1, type2 y, itype i)
  272. { return Segment_Tree::insert(Convert(x0),Convert(x1),Convert(y),Convert(i)); }
  273.  
  274. void del(type1 x0, type1 x1, type2 y)
  275. { Segment_Tree::del(Convert(x0),Convert(x1),Convert(y)); }
  276.  
  277. seg_tree_item lookup(type1 x0, type1 x1, type2 y)
  278. { return Segment_Tree::lookup(Convert(x0),Convert(x1),Convert(y)); }
  279.  
  280. int member(type1 x0, type1 x1, type2 y)
  281. { return Segment_Tree::member(Convert(x0),Convert(x1),Convert(y)); }
  282.  
  283. list<seg_tree_item> query(type1 x,type2 y0,type2 y1)
  284. { return Segment_Tree::query(Convert(x),Convert(y0),Convert(y1)); }
  285.  
  286. list<seg_tree_item> x_infinity_query(type2 y0,type2 y1)
  287. { return Segment_Tree::x_infinity_query(Convert(y0),Convert(y1)); }
  288.  
  289. list<seg_tree_item> y_infinity_query(type1 x)
  290. { return Segment_Tree::y_infinity_query(Convert(x)); }
  291.  
  292.  
  293. segment_tree()  {}
  294. ~segment_tree()
  295.  { seg_tree_item z;
  296.   forall_seg_tree_items(z,r)
  297.   { type1 t1 = x0(z); Clear(t1); 
  298.           t1 = x1(z); Clear(t1); 
  299.     type2 t2 = y(z);  Clear(t2); 
  300.     itype i  = inf(z); Clear(i); 
  301.     delete r.key(z); }
  302.  }
  303.  
  304. } ;
  305.  
  306.  
  307. //------------------------------------------------------------------
  308. // Iterator
  309. //------------------------------------------------------------------
  310.  
  311. #define forall_seg_tree_items(a,b) for ((b).init_iterator(); a=(b).move_iterator(); )
  312.  
  313.  
  314. #endif
  315.